/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.contents;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

/**
 * 
 * @author Yang Wang <yang@digitalmenubox.com>
 */
public class TableBox extends ContentBox {

	private JTable table;
	private boolean successfullyInitialized = false;

	public TableBox(Object[][] data, Object[] fields, int width, int height) {
		// setup table
		this.table = new JTable(data, fields);
		table.setOpaque(false);
		table.setEnabled(false);
		table.setFillsViewportHeight(true);
		// table.setDefaultRenderer(Object.class, new
		// TransparentBackgroundRenderer());
		table.setShowGrid(false);
		this.setRowHeight(40);

		// table = this.autoResizeColWidth(table, new DefaultTableModel(data,
		// fields));

		// Disable auto resizing
		// table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		// this.packColumns(table, 0);

		// setup table header
		table.getTableHeader().setEnabled(false);
		table.getTableHeader().setOpaque(false);
		// table.getTableHeader().setDefaultRenderer(new
		// TransparentBackgroundRenderer());
		table.setPreferredSize(new Dimension(width, height));

		// setup table box and put table into it
		this.setSize(new Dimension(width, height));
		this.setLayout(new BorderLayout());

		// To show table header, uncomment this line
		// this.add(table.getTableHeader(), BorderLayout.PAGE_START);

		this.add(table, BorderLayout.CENTER);
		this.setSuccessfullyInitialized(true);

	}

	public void setRowHeight(int height) {
		table.setRowHeight(height);
	}

	public void setRenderer(TableCellRenderer r) {
		this.table.setDefaultRenderer(Object.class, r);
	}

	public void setHeaderRenderer(TableCellRenderer r) {
		this.table.getTableHeader().setDefaultRenderer(r);
	}

	public void setColumnWidth(int columnNumber, int width) {
		TableColumn col = null;
		col = this.table.getColumnModel().getColumn(columnNumber);

		if (col == null) {
			return;
		}

		col.setPreferredWidth(width);
	}

	@Override
	public String getContent() {
		return null;
	}

	@Override
	public void cleanUp() {
	}

	@Override
	public void reinitialize() {
	}

	@Override
	public void initialize() {
	}

	public void packColumns(JTable table, int margin) {
		for (int c = 0; c < table.getColumnCount(); c++) {
			packColumn(table, c, margin);
		}
	}

	// Sets the preferred width of the visible column specified by vColIndex.
	// The column
	// will be just wide enough to show the column head and the widest cell in
	// the column.
	// margin pixels are added to the left and right
	// (resulting in an additional width of 2*margin pixels).
	public void packColumn(JTable table, int vColIndex, int margin) {
		TableModel model = table.getModel();
		DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
		TableColumn col = colModel.getColumn(vColIndex);
		int width = 0;

		// Get width of column header
		TableCellRenderer renderer = col.getHeaderRenderer();
		if (renderer == null) {
			renderer = table.getTableHeader().getDefaultRenderer();
		}
		Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
		width = comp.getPreferredSize().width;

		// Get maximum width of column data
		for (int r = 0; r < table.getRowCount(); r++) {
			renderer = table.getCellRenderer(r, vColIndex);
			comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false, r, vColIndex);
			width = Math.max(width, comp.getPreferredSize().width);
		}

		// Add margin
		width += 2 * margin;

		// Set the width
		col.setPreferredWidth(width);
	}

	public static void main(String[] args) {
		Object[][] data = new Object[3][3];
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				data[i][j] = "String" + i + " " + j;
			}
		}
		String[] fields = { "A", "B", "C" };
		final TableBox box = new TableBox(data, fields, 400, 300);
		box.setColumnWidth(0, 300);
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				JFrame f = new JFrame("test Frame");
				f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				f.getContentPane().add(box);
				// f.getContentPane().setBackground(Color.red);
				f.setPreferredSize(new Dimension(600, 400));
				f.pack();
				f.setVisible(true);
			}
		});
	}

	public JTable autoResizeColWidth(JTable table, DefaultTableModel model) {
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setModel(model);

		int margin = 5;

		for (int i = 0; i < table.getColumnCount(); i++) {
			int vColIndex = i;
			DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
			TableColumn col = colModel.getColumn(vColIndex);
			int width = 0;

			// Get width of column header
			TableCellRenderer renderer = col.getHeaderRenderer();

			if (renderer == null) {
				renderer = table.getTableHeader().getDefaultRenderer();
			}

			Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);

			width = comp.getPreferredSize().width;

			// Get maximum width of column data
			for (int r = 0; r < table.getRowCount(); r++) {
				renderer = table.getCellRenderer(r, vColIndex);
				comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false, r, vColIndex);
				width = Math.max(width, comp.getPreferredSize().width);
			}

			// Add margin
			width += 2 * margin;

			// Set the width
			col.setPreferredWidth(width);
		}

		((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(SwingConstants.LEFT);

		// table.setAutoCreateRowSorter(true);
		table.getTableHeader().setReorderingAllowed(false);

		return table;
	}

	@Override
	public boolean isSuccessfullyInitialized() {
		return this.successfullyInitialized;
	}

	public void setSuccessfullyInitialized(boolean successfullyInitialized) {
		this.successfullyInitialized = successfullyInitialized;
	}

	static class TransparentBackgroundRenderer extends JLabel implements TableCellRenderer {

		public TransparentBackgroundRenderer() {
			super();
		}

		@Override
		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
			return new JLabel(value.toString());
		}
	}

}
